var util = require('../../utils/util.js')
var app = getApp()
Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      console.log(res.target)
    }
    return {
      title: this.data.current_user.name + "邀请你加入" + this.data.subject.title,
      path: "/pages/subject/index?id=" + this.data.subject.id,
      success: function (e) {
        console.log(s)
      }
    }
  },
  data: {
    subject: {},
    current_user: {},
    id: null,
    markers: [],
    has_item: false,
    kinds: ["已接受"],
    user_type: "已接受",
    items: ["已接受"],
    form_id: null,
    comment: null,
    is_comment: false,
    attend_id: null,
  },

  onLoad: function (options) {
    var that = this
    that.setData({
      id: options.id
    })
    app.getUserInfo(function (info) {
      that.setData({
        current_user: info
      })
    })
  },
  onShow() {
    this.sendRequest(null, "GET", {})
  },
  changeSubject(e) {
    console.log(e)
  },

  bindHandle(e) {
    var list = []
    var that = this
    var master = this.data.current_user.id === this.data.subject.user_id
    if (master) {
      list = ["取消这场聚会"]
    } else {
      list = ["退出活动"]
    }
    wx.showActionSheet({
      itemList: list,
      success(res) {
        if (master) {
          //取消
          wx.request({
            url: "https://kaifan.chuanggj.com/wechat/subjects/" + that.data.id,
            method: "DELETE",
            data: { form_id: that.data.form_id },
            header: {
              "Authorization": wx.getStorageSync("token")
            },
            success(e) {
              wx.navigateTo({
                url: "/pages/index/index"
              })
            }
          })
        } else {
          that.sendRequest("cancel_subject", "POST", { form_id: that.data.form_id })
        }
      }
    })
  },
  bindConfirm(e) {
    var that = this
    if (that.data.has_item) {
      var ary = that.data.kinds
      that.setData({
        kinds: ary
      })
      wx.showActionSheet({
        itemList: ary,
        success(e) {
          that.setData({
            user_type: that.data.kinds[e.tapIndex]
          })
          that.sendRequest("join_subject", "POST", { user_type: that.data.user_type, form_id: that.data.form_id })
        }
      })
    } else {
      that.sendRequest("join_subject", "POST", { user_type: that.data.user_type, form_id: that.data.form_id })
    }
  },
  showMore(e) {
    this.toUsersInfo(null)
  },
  controltap(e) {
    var that = this
    wx.openLocation({
      latitude: this.data.subject.lat,
      longitude: this.data.subject.lon,
      scale: 14,
      name: that.data.subject.address
    })
  },
  sendRequest(url, method, args) {
    var that = this
    var base_url = "https://kaifan.chuanggj.com/wechat/subjects/" + this.data.id
    if (url && url != "null" && url != "undefined") {
      base_url = base_url + "/" + url
    }
    wx.request({
      url: base_url,
      header: {
        "Authorization": wx.getStorageSync("token")
      },
      method: method,
      data: args,
      success(res) {
        var code = res.data.code
        if (code == 200) {
          var arg = res.data.subject
          arg.week = util.formatWeek(new Date(arg.start_time * 1000))
          arg.start_time = util.formatIndex(new Date(arg.start_time * 1000))
          arg.deadline = util.formatIndex(new Date(arg.deadline * 1000))
          var ks_val = arg.kinds_type
          ks_val.push("只吃饭")
          that.setData({
            subject: arg,
            has_item: arg.kinds_type.length > 1,
            kinds: ks_val,
            items: Object.keys(arg.group_attends).filter(function (e) { return e != '发起人' }),
            markers: [{
              latitude: arg.lat,
              longitude: arg.lon
            }]
          })
        } else {
          wx.showToast({
            title: res.data.msg
          })

        }
      },
      fail: function (err) {
        wx.showToast({
          title: '连接失败'
        })
      }
    })
  },
  findUser(e) {
    var val = e.currentTarget.dataset.val
    this.toUsersInfo(val)
  },
  toUsersInfo(args) {
    var that = this
    wx.navigateTo({
      url: 'users?id=' + that.data.id + "&type=" + args
    })
  },
  showImage(event) {
    var that = this
    var current = event.currentTarget.dataset.src
    wx.previewImage({
      current: current,
      urls: that.data.subject.pictures_info,
      success(arg) {
        console.log("成功")
      },
      fail(err) {
        console.log("失败")
      }
    })
  },
  submit(e) {
    var id = e.detail.formId
    this.setData({
      form_id: id
    })
    console.log(id, this.data.form_id)
  },
  comment(e) {
    this.setData({
      is_comment: true,
      attend_id: e.currentTarget.dataset.id
    })
    console.log(e)
  },
  commentInput(e) {
    var val = e.detail.value
    var that = this
    if (val.length > 70) {
      wx.showToast({
        title: '留言字数超过限制',
      })
      return false
    } else {
      this.setData({
        is_comment: false,
        comment: val
      })
      wx.request({
        url: "https://kaifan.chuanggj.com/wechat/subjects/" + that.data.subject.id + "/comment",
        method: "POST",
        header: {
          "Authorization": wx.getStorageSync("token")
        },
        data: { content: val, attend_id: that.data.attend_id},
        success(res) {
          if (res.data.code == 200) {
            that.setData({
              subject: res.data.subject
            })
            wx.showToast({
              title: '留言成功',
            })
          }
        }
      })
    }
  },
  closeInput(e) {
    this.setData({
      is_comment: false
    })
  }
})
